package com.egao.orderB.biz.impl;

import com.egao.common.core.damon.constant.UserConstant;
import com.egao.common.core.damon.results.BaseDao;
import com.egao.common.core.damon.results.PageResult2;
import com.egao.common.core.damon.utils.Utils;
import com.egao.common.system.entity.User;
import com.egao.orderB.biz.DriverMileageBiz;
import com.egao.orderB.entity.DriverMileage;
import com.egao.common.core.web.JsonResult;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;
import java.util.List;

/**
 *
 * 数据层实现类
 * @author : Doman
 * @date : 2021-06-04 16:00:59
 * @version ：1.0
 */
@Repository
public class DriverMileageBizImpl extends BaseDao implements DriverMileageBiz {

	@Override
	public JsonResult list(PageResult2<T> page, DriverMileage driverMileage, User loginUser) throws Exception {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<DriverMileage> rm = new BeanPropertyRowMapper<DriverMileage>(DriverMileage.class);
		StringBuffer sql = new StringBuffer("select d.id,d.organization_id,d.bill_id");
        sql.append(",d.driver_id,d.driver,d.car_no,d.average_oil_consumption");
		sql.append(",d.mileage,d.mi_price,d.road_toll,d.remark");
		sql.append(",d.create_time,d.create_user,d.update_time,d.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.username createName ");
        sql.append(" from chf_driver_mileage d ");
        sql.append(" inner join sys_user suser on suser.user_id=d.create_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=d.organization_id and sogzn.deleted=0 ");
        sql.append(" where d.deleted=0 ");
		if(loginUser.getUserId() != UserConstant.ADMIN_ID){
            sql.append(" and d.organization_id=:tableOrganizationId ");
            map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
        }
    	if(Utils.isNotEmpty(driverMileage)){
			if(Utils.isNotEmpty(driverMileage.getDriver())){
				sql.append(" and instr(d.driver,:driver)>0 ");
				map.put("driver", driverMileage.getDriver());
			}
			if(Utils.isNotEmpty(driverMileage.getCarNo())){
				sql.append(" and instr(d.car_no,:car_no)>0 ");
				map.put("car_no", driverMileage.getCarNo());
			}
			if(Utils.isNotEmpty(driverMileage.getSort())&&Utils.isNotEmpty(driverMileage.getOrder())){
                sql.append(" order by d."+driverMileage.getSort()+" "+driverMileage.getOrder());
            } else {
                sql.append(" order by d.create_time desc");
            }
		}
		return jsonResult(getPageQueryList(page, sql.toString(), map, rm));
	}

	@Override
	public List<DriverMileage> list(DriverMileage driverMileage, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<DriverMileage> rm = new BeanPropertyRowMapper<DriverMileage>(DriverMileage.class);
        StringBuffer sql = new StringBuffer("select d.id,d.organization_id,d.bill_id");
        sql.append(",d.driver_id,d.driver,d.car_no,d.average_oil_consumption");
		sql.append(",d.mileage,d.mi_price,d.road_toll,d.remark");
		sql.append(",d.create_time,d.create_user,d.update_time,d.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.username createName ");
        sql.append(" from chf_driver_mileage d ");
        sql.append(" inner join sys_user suser on suser.user_id=d.create_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=d.organization_id and sogzn.deleted=0 ");
        sql.append(" where d.deleted=0 ");
		if(loginUser.getUserId() != UserConstant.ADMIN_ID){
            sql.append(" and d.organization_id=:tableOrganizationId ");
            map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
        }
    	if(Utils.isNotEmpty(driverMileage)){
//			if(Utils.isNotEmpty(driverMileage.getName())){
//				sql.append(" and instr(d.name,:name)>0 ");
//				map.put("name", driverMileage.getName());
//			}
            if(Utils.isNotEmpty(driverMileage.getSort())&&Utils.isNotEmpty(driverMileage.getOrder())){
				sql.append(" order by d."+driverMileage.getSort()+" "+driverMileage.getOrder());
			} else {
				sql.append(" order by d.create_time desc");
			}
		}
		getMapParameter(sql, map);
		return jdbcTemplate.query(sql.toString(),map, rm);
	}

	@Override
	public DriverMileage getById(DriverMileage driverMileage) {
		RowMapper<DriverMileage> rm = new BeanPropertyRowMapper<DriverMileage>(DriverMileage.class);
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql = new StringBuffer("select d.id,d.organization_id,d.bill_id");
        sql.append(",d.driver_id,d.driver,d.car_no,d.average_oil_consumption");
		sql.append(",d.mileage,d.mi_price,d.road_toll,d.remark");
		sql.append(",d.create_time,d.create_user,d.update_time,d.update_user");
        sql.append(",sogzn.organization_name organizationName,suser.username createName ");
        sql.append(" from chf_driver_mileage d ");
        sql.append(" inner join sys_user suser on suser.user_id=d.create_user ");
        sql.append(" inner join sys_organization sogzn on sogzn.organization_id=d.organization_id and sogzn.deleted=0 ");
        sql.append(" where d.deleted=0 and d.id=:id ");
		map.put("id", driverMileage.getId());
		getMapParameter(sql, map);
		return jdbcTemplate.queryForObject(sql.toString(), map, rm);
	}

	@Override
	public boolean save(DriverMileage driverMileage, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("insert into chf_driver_mileage (");
        sql.append("organization_id,bill_id,driver_id,driver");
		sql.append(",car_no,average_oil_consumption,mileage,mi_price");
		sql.append(",road_toll,remark,create_user,create_time) values ( ");
        sql.append(":organization_id,:bill_id,:driver_id,:driver");
		sql.append(",:car_no,:average_oil_consumption,:mileage,:mi_price");
		sql.append(",:road_toll,:remark,:create_user,now())");
        map.put("organization_id", loginUser.getOrganizationId() + "");
        map.put("bill_id", driverMileage.getBillId());
        map.put("driver_id", driverMileage.getDriverId()==null?null:driverMileage.getDriverId()+"");
        map.put("driver", driverMileage.getDriver());
        map.put("car_no", driverMileage.getCarNo());
        map.put("average_oil_consumption", driverMileage.getAverageOilConsumption());
        map.put("mileage", driverMileage.getMileage());
        map.put("mi_price", driverMileage.getMiPrice());
        map.put("road_toll", driverMileage.getRoadToll());
        map.put("remark", driverMileage.getRemark());
		map.put("create_user", loginUser.getUserId() + "");
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean update(DriverMileage driverMileage, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update chf_driver_mileage set ");
        sql.append("bill_id=:bill_id,driver_id=:driver_id,driver=:driver ");
		sql.append(",car_no=:car_no,average_oil_consumption=:average_oil_consumption,mileage=:mileage ");
		sql.append(",mi_price=:mi_price,road_toll=:road_toll,remark=:remark ");
		sql.append(",update_time=now(),update_user=:update_user where id=:id ");
        map.put("bill_id", driverMileage.getBillId());
        map.put("driver_id", driverMileage.getDriverId()==null?null:driverMileage.getDriverId()+"");
        map.put("driver", driverMileage.getDriver());
        map.put("car_no", driverMileage.getCarNo());
        map.put("average_oil_consumption", driverMileage.getAverageOilConsumption());
        map.put("mileage", driverMileage.getMileage());
        map.put("mi_price", driverMileage.getMiPrice());
        map.put("road_toll", driverMileage.getRoadToll());
        map.put("remark", driverMileage.getRemark());
		map.put("update_user", loginUser.getUserId() + "");
		map.put("id", driverMileage.getId());
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean remove(DriverMileage driverMileage, User loginUser) {
		int count = 0;
		Map<String, String> map = null;
		if(driverMileage.getId().length()>0){
			for(String ID : driverMileage.getId().split(",")){
				map  = new HashMap<String, String>();
				if(ID==null || ID.equals("")){ break; }
				StringBuffer sql =new StringBuffer("update chf_driver_mileage set deleted=1" +
						",update_user=:update_user, update_time=now() "
						+ " where id =:id ");
				map.put("update_user", loginUser.getUserId() + "");
				map.put("id", ID);
				getMapParameter(sql, map);
				count = jdbcTemplate.update(sql.toString(),map);
			}
		}
		return count > 0;
	}
}
